/**
 *
 * @description banner轮播视频初始化
 * @param {String} selector - css选择器
 *
 */
function banVideo(selector) {
  var $obj = $(selector);
  var autoplay = true;
  if (
    $(window).width() > 1200 &&
    $obj.find("video").length > 0
  ) {
    init();

    function init() {
      firstVideo();
      videoEnd();
      afterChange();
    }
    // 检验第一屏是否有视频
    function firstVideo() {
      var $firSlide = $obj.find(
        ".slick-slide[data-slick-index=0]"
      );
      var $video = $firSlide.find("video");
      // autoplay自动播放
      if ($video.length > 0) {
        $obj.slick("slickPause");
        // 如果轮播只有一个循环播放
        if ($obj.get(0).slick.slideCount < 2) {
          $video.attr("loop", "loop");
        }
        if ($video.get(0).paused) {
          setTimeout(function () {
            $video.get(0).play();
          }, 100);
        }
      } else {
        $obj.slick("slickPlay");
      }
    }
    // 切换到下一张是否有视频
    function afterChange() {
      $obj.on(
        "afterChange",
        function (event, slick, index) {
          // 视频暂停并回到第一帧
          $obj.find("video").each(function () {
            var video = $(this).get(0);
            video.pause();
            video.currentTime = 0;
          });
          // 有视频则暂停轮播
          var $nowSlide = $(slick.$slides[index]);
          var $video = $nowSlide.find("video");
          if ($video.length > 0) {
            $obj.slick("slickPause");
            $video.get(0).play();
          } else {
            if (autoplay) {
              $obj.slick("slickPlay");
            }
          }
        }
      );
    }
    // 视频播完切换到下一张
    function videoEnd() {
      $obj.find("video").each(function () {
        var video = $(this).get(0);
        video.onended = function () {
          if (autoplay) {
            $obj.slick("next");
            $obj.slick("slickPlay");
          }
        };
      });
    }
  } else {
    $obj.slick("play");
  }
  var banvideo = {
    play: function () {
      $obj.slick("slickPlay");
      autoplay = true;
    },
    pause: function () {
      autoplay = false;
      $obj.slick("slickPause");
    },
  };
  return banvideo;
}
/**
 *
 * @description 锚点定位
 * @param {String} reg - 路由拼接
 * @param {Number} navHeight - pc导航高度
 * @param {Number} time - 滑动的时间
 */
function yxtop(reg, navHeight, time) {
  $(window).on("load", function () {
    init(reg, navHeight, time);
  });
  function init(reg, navHeight, time) {
    var reg = reg || "tp=";
    var navHeight =
      navHeight || $(".ys-hd-pc").height() || 0;
    var time = time || 500;
    if ($(window).width() < 1200) {
      navHeight = 64;
    }
    var strArr = window.location.href.split(reg);
    if (!isNaN(strArr[1])) {
      $("html,body").animate(
        {
          scrollTop:
            $(
              '[data-page="' + strArr[1] + '"]'
            ).offset().top - navHeight,
        },
        time
      );
    }
  }
}
// 页面平滑滚动
function yxlenis() {
  lenis = new Lenis();

  lenis.on("scroll", (e) => {
    // console.log(e)
  });

  function raf(time) {
    lenis.raf(time);
    requestAnimationFrame(raf);
  }

  requestAnimationFrame(raf);
}
ys.yxlenis = yxlenis;
function lenisStop() {
  lenis.stop();
}
ys.lenisStop = lenisStop;

function lenisStart() {
  lenis.start();
}
ys.lenisStart = lenisStart;
